﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;

namespace Demo.Services.Integration.DAO.IntegrationComponents.Mappings
{
    public class ProyectDAOMapping
    {
        public static List<ProyectInfo> GetProyectMapping(DataSet ds)
        {
            List<ProyectInfo> result;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[0];

                result = (from contact in dt.AsEnumerable()
                          select new ProyectInfo
                          {
                              Id = contact.Field<int>("Id"),
                              Name  = contact.Field<string>("Nombre")
                              /*DateStart = contact.Field<string>("fechaInicio"),
                              DateEnd = contact.Field<string>("fechaFin"),
                              Hours = contact.Field<int>("horas"),
                              Active = contact.Field<bool>("activo")*/

                          }).ToList();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;

        }

        /// <summary>
        /// Obtener informaición de un DS.
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static ProjectResultInfo GetProjectList(DataSet ds)
        {
            ProjectResultInfo result;
            List<ProyectInfo> projectList;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[1];

                projectList = (from contact in dt.AsEnumerable()
                          select new ProyectInfo
                          {
                              Id = contact.Field<int>("id"),
                              Name = contact.Field<string>("nombre"),
                              DateStart = contact.Field<DateTime>("fechaInicio").ToString("dd/MM/yyyy"),
                              DateEnd = contact.Field<DateTime>("fechaFin").ToString("dd/MM/yyyy"),
                              Hours = contact.Field<int>("horas"),
                              StatusDescription = contact.Field<string>("descripcion"),                              
                          }).ToList();
                result = new ProjectResultInfo();
                result.ProjectList = projectList;
                result.TotalReg = Convert.ToInt32(ds.Tables[0].Rows[0]["TotalReg"]);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public static ProyectInfo GetProjectById(DataSet ds)
        {
            ProyectInfo result;
            try
            {
                Logger.Info();
                DataTable dt = ds.Tables[0];

                result = (from contact in dt.AsEnumerable()
                          select new ProyectInfo
                          {
                              Id = contact.Field<int>("id"),
                              Name = contact.Field<string>("nombre"),
                              DateStart = contact.Field<DateTime>("fechaInicio").ToString("dd/MM/yyyy"),
                              DateEnd = contact.Field<DateTime>("fechaFin").ToString("dd/MM/yyyy"),
                              Hours = contact.Field<int>("horas"),
                              StatusId = contact.Field<int>("estatusId")

                          }).ToList()[0];
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;

        }

        public static  List<ProjectReportInfo> GetProjectStatus(DataSet ds)
        {
            List<ProjectReportInfo> result;
            DataRow dataRow;
            try
            {
                Logger.Info();
                result = new List<ProjectReportInfo>();
                dataRow = ds.Tables[0].Rows[0];
                result.Add(GetProject(dataRow,1));
                result.Add(GetProject(dataRow,2));
                result.Add(GetProject(dataRow,3));
                result.Add(GetProject(dataRow,4));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;

        }


        private static ProjectReportInfo GetProject(DataRow row,int index)
        {
            ProjectReportInfo project;
            try
            {
                project = new ProjectReportInfo();
                switch (index)
                {
                    case 1:
                        project.Name = "Proceso";
                        project.ResourcesNumber = (int)row["proceso"];
                        break;
                    case 2:
                        project.Name = "Suspendido";
                        project.ResourcesNumber = (int)row["suspendido"];
                        break;
                    case 3:
                        project.Name = "Cerrado";
                        project.ResourcesNumber = (int)row["cerrado"];
                        break;
                    case 4:
                        project.Name = "Espera";
                        project.ResourcesNumber = (int)row["espera"];
                        break;
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return project;
        }

    }
	
	 
}
